package com.face.gaoml.netlibrary.data.init;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.text.TextUtils;


import com.face.gaoml.netlibrary.utils.Util;

import java.util.UUID;

import javax.inject.Inject;


public class SystemConfig {

    private final static String SP_KEY = "HBSP";
    public String version;                          // 版本号
    public String os = "2";                         // 平台系统 默认：android 1苹果 2安卓
    public String device_name;                      //手机型号
    public String device_system_version;            //手机系统版本
    public String network_type = "";                     //手机系统版本
    private String appID;                           // 客户端唯一标示
    private String cooke;
    private static SystemConfig sc;
    private Application mApplication;


    @Inject
    public SystemConfig(Application mApplication ) {
        this.mApplication = mApplication;
        getVersion();
        device_name = Build.MODEL.replace("[", "").replace(" ", "").replace("]", "");
//        if(device_name.length()>6){
//            device_name= device_name.substring(0,6);
//        }
        device_system_version = Build.VERSION.SDK;
        network_type = Util.GetNetworkType(mApplication);
    }

//    public static synchronized SystemConfig getInstance() {
//        if (sc == null) {
//            sc = new SystemConfig();
//            // 配置默认值
//        }
//
//        return sc;
//    }

    /**
     * 获取 APP版本号
     */
    private void getVersion() {
        try {
            version = mApplication.getPackageManager()
                    .getPackageInfo(
                            mApplication.getPackageName(), 0).versionName;
        } catch (NameNotFoundException e) {
            version = "";
        }
    }


    /**
     * 获取、生成、保存appID
     *
     * @return
     */
    private final static String KEY_APPID = "APPID";

    public String getAppID() {
        SharedPreferences sp = mApplication.getSharedPreferences(
                SP_KEY, 0);
        if (appID != null || (appID = sp.getString(KEY_APPID, null)) != null) {
            return appID;
        }
        appID = UUID.randomUUID().toString();
        sp.edit().putString(KEY_APPID, appID).commit();
        return appID;
    }

    /*
     * 获取 cooke
     */
    private final static String KEY_COOKE = "HB_APPID";

    public String getCooke() {
        if (TextUtils.isEmpty(cooke)) {
            SharedPreferences sp = mApplication.getSharedPreferences(
                    SP_KEY, 0);
            return sp.getString(KEY_COOKE, "");
        } else {
            return cooke;
        }
    }

    public void setCooke(String cookes) {

        if (cookes != null && !cookes.equals(cooke)) {
            SharedPreferences sp = mApplication.getSharedPreferences(
                    SP_KEY, 0);
            sp.edit().putString(KEY_COOKE, cookes).commit();
            this.cooke = cookes;
        }


    }
}
